﻿using System.Text;

namespace MyCrawlerDemo;

public class HttpClientHelper
{
    public static string Post(string url) {
        string result = "";
        using (HttpClient client = new()) {
            using (StringContent content = new("", Encoding.UTF8)) {
                var response = client.PostAsync(url, content).Result;
                result = response.Content.ReadAsStringAsync().Result;
            }
        }
        return result;
    }

    public static string PostUseAutoCookie(string url)
    {
        string result = "";
        using HttpClientHandler handler = new()
        {
            UseCookies = true
        };
        using HttpClient client = new(handler);
        using StringContent content = new("", Encoding.UTF8);
        var response = client.PostAsync(url, content).Result;
        result = response.Content.ReadAsStringAsync().Result;
        return result;
    }

    public static string PostUseHandleCookie(string url, string cookie)
    {
        string result = "";
        using HttpClientHandler handler = new()
        {
            UseCookies = false
        };
        using HttpClient client = new(handler);
        using StringContent content = new("", Encoding.UTF8);
        using HttpRequestMessage message = new(HttpMethod.Post, url);
        var response = client.SendAsync(message).Result;
        result = response.Content.ReadAsStringAsync().Result;
        return result;
    }
}
